home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / dev / gg / gengetopt-2.6.lha / gengetopt-2.6 / doc / cmdline2.c < prev    next >
Text File  |  2002-06-24  |  9KB  |  302 lines

  1. /*
  2.   File autogenerated by gengetopt version 2.6  
  3.   generated with the following command:
  4.   ../src/gengetopt --input=sample2.ggo --func-name=my_cmdline_parser --file-name=cmdline2 --unamed-opts --no-handle-help --no-handle-version 
  5.  
  6.   The developers of gengetopt consider the fixed text that goes in all
  7.   gengetopt output files to be in the public domain:
  8.   we make no copyright claims on it.
  9. */
  10.  
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. /* If we use autoconf.  */
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. /* Check for configure's getopt check result.  */
  20. #ifndef HAVE_GETOPT_LONG
  21. #include "getopt.h"
  22. #else
  23. #include <getopt.h>
  24. #endif
  25.  
  26. #ifndef HAVE_STRDUP
  27. #define strdup gengetopt_strdup
  28. #endif /* HAVE_STRDUP */
  29.  
  30. #include "cmdline2.h"
  31.  
  32.  
  33. void
  34. my_cmdline_parser_print_version (void)
  35. {
  36.   printf ("%s %s\n", PACKAGE, VERSION);
  37. }
  38.  
  39. void
  40. my_cmdline_parser_print_help (void)
  41. {
  42.   my_cmdline_parser_print_version ();
  43.   printf("\n"
  44. "Purpose:\n"
  45. "  this is just a test program for gengetopt\n"
  46. "\n"
  47. "Usage: %s [OPTIONS]... [FILES]...\n", PACKAGE);
  48.   printf("   -h             --help                        Print help and exit\n");
  49.   printf("   -V             --version                     Print version and exit\n");
  50.   printf("   -sSTRING       --str-opt=STRING              A string option\n");
  51.   printf("   -iINT          --int-opt=INT                 A int option\n");
  52.   printf("   -SSHORT        --short-opt=SHORT             A short option\n");
  53.   printf("   -lLONG         --long-opt=LONG               A long option\n");
  54.   printf("   -fFLOAT        --float-opt=FLOAT             A float option\n");
  55.   printf("   -dDOUBLE       --double-opt=DOUBLE           A double option\n");
  56.   printf("   -LLONGDOUBLE   --long-double-opt=LONGDOUBLE  A long double option\n");
  57.   printf("   -yLONGLONG     --long-long-opt=LONGLONG      A long long option\n");
  58.   printf("   -F             --func-opt                    A function option\n");
  59.   printf("   -x             --flag-opt                    A flag option (default=off)\n");
  60. }
  61.  
  62.  
  63. (null)
  64. èt0eL
  65.  
  66. Ì
  67.  
  68.  
  69. (null)
  70. 
  71.  
  72. Undefined error: 0
  73. Operation not permitted
  74.  
  75. int
  76. my_cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info)
  77. {
  78.   int c;    /* Character of the parsed option.  */
  79.   int missing_required_options = 0;    
  80.  
  81.   args_info->help_given = 0 ;
  82.   args_info->version_given = 0 ;
  83.   args_info->str_opt_given = 0 ;
  84.   args_info->int_opt_given = 0 ;
  85.   args_info->short_opt_given = 0 ;
  86.   args_info->long_opt_given = 0 ;
  87.   args_info->float_opt_given = 0 ;
  88.   args_info->double_opt_given = 0 ;
  89.   args_info->long_double_opt_given = 0 ;
  90.   args_info->long_long_opt_given = 0 ;
  91.   args_info->func_opt_given = 0 ;
  92.   args_info->flag_opt_given = 0 ;
  93. #define clear_args() { \
  94.   args_info->str_opt_arg = NULL; \
  95.   args_info->flag_opt_flag = 0;\
  96. }
  97.  
  98.   clear_args();
  99.  
  100.   args_info->inputs = NULL;
  101.   args_info->inputs_num = 0;
  102.  
  103.   optarg = 0;
  104.   optind = 1;
  105.   opterr = 1;
  106.   optopt = '?';
  107.  
  108.   while (1)
  109.     {
  110.       int option_index = 0;
  111.       static struct option long_options[] = {
  112.         { "help",    0, NULL, 'h' },
  113.         { "version",    0, NULL, 'V' },
  114.         { "str-opt",    1, NULL, 's' },
  115.         { "int-opt",    1, NULL, 'i' },
  116.         { "short-opt",    1, NULL, 'S' },
  117.         { "long-opt",    1, NULL, 'l' },
  118.         { "float-opt",    1, NULL, 'f' },
  119.         { "double-opt",    1, NULL, 'd' },
  120.         { "long-double-opt",    1, NULL, 'L' },
  121.         { "long-long-opt",    1, NULL, 'y' },
  122.         { "func-opt",    0, NULL, 'F' },
  123.         { "flag-opt",    0, NULL, 'x' },
  124.         { NULL,    0, NULL, 0 }
  125.       };
  126.  
  127.       c = getopt_long (argc, argv, "hVs:i:S:l:f:d:L:y:Fx", long_options, &option_index);
  128.  
  129.       if (c == -1) break;    /* Exit from `while (1)' loop.  */
  130.  
  131.       switch (c)
  132.         {
  133.         case 'h':    /* Print help and exit.  */
  134.           if (args_info->help_given)
  135.             {
  136.               fprintf (stderr, "%s: `--help' (`-h') option given more than once\n", PACKAGE);
  137.               clear_args ();
  138.               exit (EXIT_FAILURE);
  139.             }
  140.           args_info->help_given = 1;
  141.           return 0;
  142.  
  143.         case 'V':    /* Print version and exit.  */
  144.           if (args_info->version_given)
  145.             {
  146.               fprintf (stderr, "%s: `--version' (`-V') option given more than once\n", PACKAGE);
  147.               clear_args ();
  148.               exit (EXIT_FAILURE);
  149.             }
  150.           args_info->version_given = 1;
  151.           return 0;
  152.  
  153.         case 's':    /* A string option.  */
  154.           if (args_info->str_opt_given)
  155.             {
  156.               fprintf (stderr, "%s: `--str-opt' (`-s') option given more than once\n", PACKAGE);
  157.               clear_args ();
  158.               exit (EXIT_FAILURE);
  159.             }
  160.           args_info->str_opt_given = 1;
  161.           args_info->str_opt_arg = strdup (optarg);
  162.           break;
  163.  
  164.         case 'i':    /* A int option.  */
  165.           if (args_info->int_opt_given)
  166.             {
  167.               fprintf (stderr, "%s: `--int-opt' (`-i') option given more than once\n", PACKAGE);
  168.               clear_args ();
  169.               exit (EXIT_FAILURE);
  170.             }
  171.           args_info->int_opt_given = 1;
  172.           args_info->int_opt_arg = atoi (optarg);
  173.           break;
  174.  
  175.         case 'S':    /* A short option.  */
  176.           if (args_info->short_opt_given)
  177.             {
  178.               fprintf (stderr, "%s: `--short-opt' (`-S') option given more than once\n", PACKAGE);
  179.               clear_args ();
  180.               exit (EXIT_FAILURE);
  181.             }
  182.           args_info->short_opt_given = 1;
  183.           args_info->short_opt_arg = (short)atoi (optarg);
  184.           break;
  185.  
  186.         case 'l':    /* A long option.  */
  187.           if (args_info->long_opt_given)
  188.             {
  189.               fprintf (stderr, "%s: `--long-opt' (`-l') option given more than once\n", PACKAGE);
  190.               clear_args ();
  191.               exit (EXIT_FAILURE);
  192.             }
  193.           args_info->long_opt_given = 1;
  194.           args_info->long_opt_arg = atol (optarg);
  195.           break;
  196.  
  197.         case 'f':    /* A float option.  */
  198.           if (args_info->float_opt_given)
  199.             {
  200.               fprintf (stderr, "%s: `--float-opt' (`-f') option given more than once\n", PACKAGE);
  201.               clear_args ();
  202.               exit (EXIT_FAILURE);
  203.             }
  204.           args_info->float_opt_given = 1;
  205.           args_info->float_opt_arg = (float)strtod (optarg, NULL);
  206.           break;
  207.  
  208.         case 'd':    /* A double option.  */
  209.           if (args_info->double_opt_given)
  210.             {
  211.               fprintf (stderr, "%s: `--double-opt' (`-d') option given more than once\n", PACKAGE);
  212.               clear_args ();
  213.               exit (EXIT_FAILURE);
  214.             }
  215.           args_info->double_opt_given = 1;
  216.           args_info->double_opt_arg = strtod (optarg, NULL);
  217.           break;
  218.  
  219.         case 'L':    /* A long double option.  */
  220.           if (args_info->long_double_opt_given)
  221.             {
  222.               fprintf (stderr, "%s: `--long-double-opt' (`-L') option given more than once\n", PACKAGE);
  223.               clear_args ();
  224.               exit (EXIT_FAILURE);
  225.             }
  226.           args_info->long_double_opt_given = 1;
  227.           args_info->long_double_opt_arg = (long double) strtod (optarg, NULL);
  228.           break;
  229.  
  230.         case 'y':    /* A long long option.  */
  231.           if (args_info->long_long_opt_given)
  232.             {
  233.               fprintf (stderr, "%s: `--long-long-opt' (`-y') option given more than once\n", PACKAGE);
  234.               clear_args ();
  235.               exit (EXIT_FAILURE);
  236.             }
  237.           args_info->long_long_opt_given = 1;
  238.           args_info->long_long_opt_arg = (long long)atol (optarg);
  239.           break;
  240.  
  241.         case 'F':    /* A function option.  */
  242.           if (args_info->func_opt_given)
  243.             {
  244.               fprintf (stderr, "%s: `--func-opt' (`-F') option given more than once\n", PACKAGE);
  245.               clear_args ();
  246.               exit (EXIT_FAILURE);
  247.             }
  248.           args_info->func_opt_given = 1;
  249.           break;
  250.  
  251.         case 'x':    /* A flag option.  */
  252.           if (args_info->flag_opt_given)
  253.             {
  254.               fprintf (stderr, "%s: `--flag-opt' (`-x') option given more than once\n", PACKAGE);
  255.               clear_args ();
  256.               exit (EXIT_FAILURE);
  257.             }
  258.           args_info->flag_opt_given = 1;
  259.           args_info->flag_opt_flag = !(args_info->flag_opt_flag);
  260.           break;
  261.  
  262.         case 0:    /* Long option with no short option */
  263.  
  264.         case '?':    /* Invalid option.  */
  265.           /* `getopt_long' already printed an error message.  */
  266.           exit (EXIT_FAILURE);
  267.  
  268.         default:    /* bug: option not considered.  */
  269.           fprintf (stderr, "%s: option unknown: %c\n", PACKAGE, c);
  270.           abort ();
  271.         } /* switch */
  272.     } /* while */
  273.  
  274.   if (! args_info->int_opt_given)
  275.     {
  276.       fprintf (stderr, "%s: `--int-opt' (`-i') option required\n", PACKAGE);
  277.       missing_required_options = 1;
  278.     }
  279.  
  280.   if (! args_info->long_opt_given)
  281.     {
  282.       fprintf (stderr, "%s: `--long-opt' (`-l') option required\n", PACKAGE);
  283.       missing_required_options = 1;
  284.     }
  285.  
  286.   if ( missing_required_options )
  287.     exit (EXIT_FAILURE);
  288.  
  289.   if (optind < argc)
  290.     {
  291.       int i = 0 ;
  292.  
  293.       args_info->inputs_num = argc - optind ;
  294.       args_info->inputs = 
  295.         (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
  296.       while (optind < argc)
  297.         args_info->inputs[ i++ ] = strdup (argv[optind++]) ; 
  298.     }
  299.  
  300.   return 0;
  301. }
  302.